//
//  UserEditEmotionalController.m
//  CheekiOS
//
//  Created by mac on 2017/7/5.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "UserEditEmotionalController.h"
#import "UserEditEmotionalCell.h"

@interface UserEditEmotionalController ()<UITableViewDelegate, UITableViewDataSource, UserEditEmotionalCellDelegate>
@property (nonatomic, strong)NSArray *nameArray;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSString *emotionalStr;
@end

@implementation UserEditEmotionalController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self selfTitleFontColor];
    self.title = @"情感";
    _emotionalStr = @"";
    UIButton *RightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [RightBtn addTarget:self action:@selector(RightBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [RightBtn setTitle:@"保存" forState:UIControlStateNormal];
    RightBtn.frame = CGRectMake(0, 0, 40, 40);
    [RightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    RightBtn.titleLabel.font = FontP(15);
    UIBarButtonItem *RightItem = [[UIBarButtonItem alloc]initWithCustomView:RightBtn];
    self.navigationItem.rightBarButtonItem = RightItem;
    
    
    if (!_tableView) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-49) style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }

}
- (NSArray *)nameArray{

    if (!_nameArray) {
        NSArray * titleArr =  @[@"单身",@"貌似恋爱",@"恋爱",@"已婚",@"离异"];
        NSMutableArray * dataArr = [NSMutableArray array];
        
        for (int i = 0; i < titleArr.count; i++) {
            UserEditEmotinalModel * model = [UserEditEmotinalModel new];
            model.title = titleArr[i];
            model.isSelect = NO;
            [dataArr addObject:model];
        }
        _nameArray = dataArr;
    }
    return _nameArray;
}
- (void)RightBtnAction{

    NSDictionary *dict =[NSDictionary dictionary];
    dict = @{@"uid":[HQHttpManager getUserId],
             @"affection":_emotionalStr
             };
    
    [HcardHttpmanager MakePersionalInfoWithParam:dict success:^(id sucess) {
        if(sucess){
            NSLog(@"设置成功");
            [SVProgressHUD showWithStatus:@"上传成功"];
            
            [SVProgressHUD dismissWithDelay:0.8];
            
            
            NSString *Str = @"NotificationUserCoverimageUpdate";
            [[NSNotificationCenter defaultCenter] postNotificationName:Str object:nil
             ];
            [self.navigationController popViewControllerAnimated:YES];
        }
    } failure:^(NSError *error) {
        
    }];


}

- (void)userEditEmotinalCell:(UserEditEmotionalCell *)cell clickBtn:(UIButton *)sender{
    NSLog(@"%ld",sender.tag);
    for (UserEditEmotinalModel * model in self.nameArray) {
        if (model == cell.model) {
            model.isSelect = YES;
        }else{
            model.isSelect = NO;
        }
    }
    [self.tableView reloadData];

    _emotionalStr = [_nameArray[sender.tag] valueForKey:@"title"];
    
    NSLog(@"%@",_emotionalStr);
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.nameArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 10;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UserEditEmotionalCell *cell = [UserEditEmotionalCell cellWithTableView:tableView indexPath:indexPath];
    cell.model = self.nameArray[indexPath.row];
    cell.delegate = self;
    return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 55;
}

@end
